/**
 * Created by Administrator on 2017/4/13.
 */

var tool = require('../../common/utils/appUtils');
var async = require('async');
var moment = require('moment');
moment.locale('zh-cn');
module.exports = {

    put2DataBase: function (param, callback) {
        tool.baseDao('put2Database', 'insert into cdnxmlbody values(?,?)', param, function (result) {
            callback(result);
        })
    },
    put2DataBaseLiveContent: function (param, callback) {
        async.waterfall([
            function (callBack) {
                tool.baseDao('1', 'select * from livecategory where lcname=?', param['lcname'], function (result) {
                    if (result.isSuccess && result.rows.length > 0) {
                        callBack(null, result['rows'][0]['lcid'])
                    } else {
                        // 添加
                        // tool.baseDao('1','select * from livecategory where lcname=?',param['lcname'],function (result) {
                        //     if(result.isSuccess&&result.rows.length>0) {
                        //         callBack(null,result['rows'][0]['lcid'])
                        //     }else{
                        //         // 添加
                        //         tool.baseDao('0','insert into livecategory (lcname,lcimgurl,createtime) values(?,?,?)',[param['lcname'],param['lcimgurl'],param['createtime']],function (reslut) {
                        //
                        //             callBack(null,'')
                        //         })
                        //         // callBack()
                        //     }
                        // })
                        tool.baseDao('0', 'insert into livecategory (lcname,lcimgurl,createtime) values(?,?,?)', [param['lcname'], param['lcimgurl'], param['createtime']], function (reslut) {
                            callBack(null, '')
                        })
                        // callBack()
                    }
                })
            },
            function (arg, callBack) {
                tool.baseDao('', 'select * from livecontent where id=?', param['id'], function (result) {
                    if (result.isSuccess && result.length > 0) {
                        //有数据
                    } else {
                        //无数据添加
                        tool.baseDao('live', 'insert into cdnlivecontent (id,lcid,ltid,livecontent,opentime,endtime,duration,livememo) values(?,?,1,?,?,?,?,?)', [param['id'], arg, param['livecontent'], param['opentime'], param['endtime'], param['duration'], param['livememo']], function (result) {
                            callBack(null, result);
                        })
                    }
                });
            }
        ], function (err, result) {

            callback(result)
        })

        // tool.baseDao('put2DataBaseLiveContent','insert into livecontent values(lcid,)',param,function (result) {
        //     callback(result);
        // })
    },

    put2DataBaseLiveContentSimple: function (param, callback) {
        tool.baseDao('live', 'insert into cdnlivecontent  values(?,?,?,?,?,?,?,?,?,?)', [param['id'], param['lcname'], param['lcimgurl'], param['ltname'], param['lturl'], param['livecontent'], param['livememo'], param['opentime'], param['endtime'], param['duration']], function (result) {
            callback(result);
        })
    },
    getLiveContentByTypeParam: function (param, callback) {
        //参数为  日期 页数
        // var date=param[0];
        var type = param[0];
        var page = (parseInt(param[1]) - 1) * 10;
        // var date = moment().format('YYYYMMDD').toString();

        async.parallel([

            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime asc', [type, page -3, page -4], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            week:moment().add(page + 4, 'days').format('dddd'),
                            flag:'',
                            date: moment().add(page + 4, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page -2, page -3], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            // date:'后天',
                            flag:'',
                            week:moment().add(page + 3, 'days').format('dddd'),
                            date: moment().add(page + 3, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page -1, page -2], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag: '',
                            week:moment().add(page + 2, 'days').format('dddd'),
                            date: moment().add(page + 2, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page , page-1], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag: '明天',
                            week:'明天',
                            date: moment().add(page + 1, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page + 1, page], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag: '今天',
                            week: '今天',
                            date: moment().subtract(page, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page + 2, page + 1], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag:'',
                            week:moment().subtract(page + 1, 'days').format('dddd'),
                            date: moment().subtract(page + 1, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page + 3, page + 2], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag: '',
                            week:moment().subtract(page + 2, 'days').format('dddd'),
                            date: moment().subtract(page + 2, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page + 4, page + 3], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag:'',
                            week:moment().subtract(page + 3, 'days').format('dddd'),
                            date: moment().subtract(page + 3, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page + 5, page + 4], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag:'',
                            week:moment().subtract(page + 4, 'days').format('dddd'),
                            date: moment().subtract(page + 4, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE lcname=? and  TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [type, page + 6, page + 5], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            flag:'',
                            week:moment().subtract(page + 5, 'days').format('dddd'),
                            date: moment().subtract(page + 5, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            }

        ], function (err, reslut) {
            callback(reslut)
        });
    },
    getLiveContentByType: function (param, callback) {
        //参数为  日期 页数
        // var date=param[0];
        // var type=param[1];
        var page = (parseInt(param) - 1) * 5;
        // var date = moment().format('YYYYMMDD').toString();

        async.parallel([
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [page + 1, page], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            date: moment().subtract(page, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [page + 2, page + 1], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            date: moment().subtract(page + 1, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [page + 3, page + 2], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            date: moment().subtract(page + 2, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [page + 4, page + 3], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            date: moment().subtract(page + 3, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            },
            function (callback) {
                tool.baseDao('getLiveContentByType', 'SELECT * FROM cdnlivecontent WHERE TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < ? and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >=? ORDER BY opentime', [page + 5, page + 4], function (reslut) {
                    if (reslut.isSuccess) {
                        callback(null, {
                            date: moment().subtract(page + 4, 'days').format('YYYYMMDD').toString(),
                            data: reslut.rows
                        });
                    } else {
                        callback(reslut);
                    }
                })
            }

        ], function (err, reslut) {
            callback(reslut)
        });
    },

    getLiveCategory: function (param, callback) {
        tool.baseDao('live', 'select lcname from cdnlivecontent WHERE TO_DAYS( NOW( ) ) - TO_DAYS( opentime) < 6 and TO_DAYS( NOW( ) ) - TO_DAYS( opentime) >= -4 GROUP BY lcname ', null, function (result) {
            callback(result);
        })
    }


}